/**
 *用户管理需要的js文件
 */

layui.use('table', function () {
    var table = layui.table;
    table.render({
        elem: '#User',
        height: 500,
        cellMinWidth: 100,
        url: '/manager/user.do', // 数据接口
        id: 'user',
        page: true, // 开启分页

        cols: [[ // 表头
            {
                field: 'userId',
                title: '用户编号',
                sort: true,
                align: 'center'
            }, {
                field: 'userName',
                title: '用户名称',
                align: 'center'
            }, {
                field: 'email',
                title: '用户邮箱',
                align: 'center'
            }, {
                field: 'phone',
                title: '用户电话',
                align: 'center'
            }, {
                field: 'sex',
                title: '性别',
                sort: true,
                align: 'center',
                templet: function (value) {
                    if (value.sex == "1") {
                        return '男'
                    } else {
                        return '女'
                    }
                }
            }, {
                field: 'address',
                title: '地址',
                align: 'center'
            }, {
                field: 'integration',
                title: '积分',
                align: 'center',
                sort: true
            }, {
                field: 'joinDate',
                title: '加入日期',
                align: 'center',
                sort: true
            }, {
                fixed: 'right',
                title: '操作',
                align: 'center',
                toolbar: '#barDemo',
                width: 150
            }]]
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        console.log(data);
        var userId=data.userId;
        if (obj.event === 'checkEssay') {
            var userId = data.userId;
            parent.frames["main"].window.location.href = "/manager/checkUser.do?userId=" + userId;
        }else if(obj.event=='delete'){
            layer.confirm('真的删除用户吗,删除后不可撤销', function (index) {
                $.ajax({
                    type: 'POST',
                    url: '/manager/deleteUser.do',
                    data: {
                        userId: userId,
                    },
                    success: function (data) {
                        if (data == "success") {
                            layer.msg("操作成功", {
                                icon: 1,
                                time: 500
                            }, function () {
                               /* window.location.href = "/manager/messageredirect.do"*/
                                obj.del();
                                layer.close(index);
                            });
                        } else if (data == "false") {
                            layer.msg("服务器好像出错了，请稍后重试", {
                                icon: 2,
                                time: 500
                            }, function () {
                                window.location.href = "/manager/userredirect.do"
                            });
                        }
                    }
                });
            });
        }
    });

    //表格重载数据/根据ID查找
    $("#searchId").click(function () {
        var username = $("#username").val();
        var userId = $("#userId").val();
        table.reload('user', {
            url: '/manager/userSelect.do',
            where: {
                userId: userId,
                username: username
            }
        });
    });

    //表格重载数据/根据用户名查找
    $("#searchUsername").click(function () {
        var userId = $("#userId").val();
        var username = $("#username").val();
        table.reload('user', {
            url: '/manager/userSelect.do',
            where: {
                userId: userId,
                username: username
            }
        });
    });
});